package com.xdf.docmanagementsystem.service.impl;

import com.xdf.docmanagementsystem.common.util.Example;
import com.xdf.docmanagementsystem.enums.ResponseEnum;
import com.xdf.docmanagementsystem.service.ValidateService;
import com.xdf.docmanagementsystem.vo.TreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xdf.docmanagementsystem.entity.Department;

import java.util.ArrayList;
import java.util.List;

import com.xdf.docmanagementsystem.dao.DepartmentMapper;
import com.xdf.docmanagementsystem.service.DepartmentService;
import org.springframework.util.Assert;

/**
 * @Description : com.xdf.docmanagementsystem.service.impl(业务层实现类,该类为生成器自动生成).
 * @Author : ZK
 * @Date : 2020-06-20 13:34:16
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper dao;
    @Autowired
    @Qualifier("departmentValidateServiceImpl")
    private ValidateService validateService;

    /**
     * @Describe 新增方法
     * @author ZK
     * @Date 2020-06-20 13:34:16
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(Department record) throws Exception {
        Assert.state(validateService.shouldInsert(record), ResponseEnum.NAME_DUMP.getMessage());
        return dao.insert(record);
    }

    /**
     * @Describe 选择新增
     * @author ZK
     * @Date 2020-06-20 13:34:16
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(Department record) throws Exception {
        Assert.state(validateService.shouldInsert(record), ResponseEnum.NAME_DUMP.getMessage());
        return dao.insertSelective(record);
    }

    /**
     * @Describe 批量新增
     * @author ZK
     * @Date 2020-06-20 13:34:16
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<Department> list) throws Exception {
        return dao.multiInsert(list);
    }

    /**
     * @Describe 修改
     * @author ZK
     * @Date 2020-06-20 13:34:16
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(Department record) throws Exception {
        if (record.getDetartmentStatus() != null && record.getDetartmentStatus() == 0) {
            Assert.state(validateService.shouldDelete(record), ResponseEnum.NOT_ALLOW.getMessage());
        } else {
            Assert.state(validateService.shouldUpdate(record), ResponseEnum.NOT_ALLOW.getMessage());
        }
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 选择修改
     * @author ZK
     * @Date 2020-06-20 13:34:16
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(Department record) throws Exception {
        if (record.getDetartmentStatus() != null && record.getDetartmentStatus() == 0) {
            Assert.state(validateService.shouldDelete(record), ResponseEnum.NOT_ALLOW.getMessage());
        } else {
            Assert.state(validateService.shouldUpdate(record), ResponseEnum.NOT_ALLOW.getMessage());
        }
        return dao.updateByPrimaryKeySelective(record);
    }

    /**
     * @Describe 修改
     * @author ZK
     * @Date 2020-06-20 13:34:16
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(Department record) throws Exception {
        Assert.state(validateService.shouldUpdate(record), ResponseEnum.NOT_ALLOW.getMessage());
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 批量修改
     * @author ZK
     * @Date 2020-06-20 13:34:16
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiUpdate(List<Department> list) throws Exception {
        return dao.multiUpdate(list);
    }

    /**
     * @Describe 根据主键查询
     * @author ZK
     * @Date 2020-06-20 13:34:16
     */
    @Override
    public Department selectByPrimaryKey(Integer id) throws Exception {
        return dao.selectByPrimaryKey(id);
    }

    /**
     * @Describe 根据主键删除
     * @author ZK
     * @Date 2020-06-20 13:34:16
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByPrimaryKey(Integer id) throws Exception {
        return dao.deleteByPrimaryKey(id);
    }

    /**
     * @Describe 根据Entity查询
     * @author ZK
     * @Date 2020-06-20 13:34:16
     */
    @Override
    public List<Department> listQuery(Department record) throws Exception {
        return dao.selectByParam(record);
    }

    /**
     * 获取树
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<TreeVo> getTreeVoList(Integer parentId) throws Exception {
        Example example;
        if (parentId != null) {
            example = new Example().addWhereCondition(
                    Example.whereCondition("department_parent_id =", parentId))
                    .andEq("detartment_status", 1);
        } else {
            example = new Example().addWhereCondition(
                    Example.whereCondition("ISNULL(", "department_parent_id)"))
                    .andEq("detartment_status", 1);
        }
        List<TreeVo> list = new ArrayList<>();
        List<Department> rootNodes = dao.selectByExample(example);
        if (rootNodes == null) return null;
        for (Department department : rootNodes) {
            TreeVo treeVo = TreeVo.builder()
                    .id(String.valueOf(department.getId()))
                    .text(department.getDepartmentName())
                    .parentId(String.valueOf(parentId))
                    .build();
            List<Department> children = dao.selectByParam(
                    Department.builder()
                            .departmentParentId(department.getId())
                            .detartmentStatus(1).build());
            if (children != null && children.size() > 0) {
                treeVo.setChildren(getTreeVoList(department.getId()));
            }
            list.add(treeVo);
        }
        return list;
    }

    /**
     * 获取子节点
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public List<Integer> findChildrenId(Integer id) throws Exception {
        if (id == null) return null;
        List<Integer> result = new ArrayList<>();
        result.add(id);
        List<Department> children = dao.selectByParam(Department.builder().detartmentStatus(1).departmentParentId(id).build());
        if (children != null) {
            for (Department department : children) {
                List<Integer> childItem = findChildrenId(department.getId());
                if (childItem != null) {
                    result.addAll(childItem);
                }
            }
        }
        return result;
    }

}